package com.qianfeng.smsplatform.strategy.service.impl;//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                  佛祖镇楼                  BUG辟易
//          佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//


import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.common.constants.RabbitMqConsants;
import com.qianfeng.smsplatform.common.constants.StrategyConstants;
import com.qianfeng.smsplatform.common.model.Standard_Report;
import com.qianfeng.smsplatform.common.model.Standard_Submit;
import com.qianfeng.smsplatform.strategy.feign.RedisCacheFeign;
import com.qianfeng.smsplatform.strategy.mq.SendMq;
import com.qianfeng.smsplatform.strategy.service.FilterChain;
import com.qianfeng.smsplatform.strategy.service.ReportMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 扣费
 * Created by Mr.ycz on 2019/12/3 9:44
 */
@Service
public class SmsFeeFilter implements FilterChain {
    private final static Logger log = LoggerFactory.getLogger(SmsBlackFilter.class);

    @Autowired
    private RedisCacheFeign cacheFeign;
    @Autowired
    private SendMq sendMq;

    @Value("${Filter.fee}")
    private Long limitFee;

    @Override
    public boolean dealMessage(Standard_Submit submit) {

        //取得短信单价
        Map<Object, Object> router = cacheFeign.hmget(CacheConstants.CACHE_PREFIX_ROUTER + submit.getClientID());
        long price = 0;

        if (router != null) {
            Object priceStr = router.get("price");
            price = Long.parseLong(priceStr.toString());
        } else {
            log.error("没有查询到用户id为{}的短信单价", submit.getClientID());
            return false;
        }

        //查询用户费用为多少 单位为厘 小于100000提醒用户充值
        String feeStr = cacheFeign.getString(CacheConstants.CACHE_PREFIX_CUSTOMER_FEE + submit.getClientID());
        if (feeStr == null || feeStr.length() == 0) {
            log.info("没有查询到用户id为{}的剩余金额", submit.getClientID());
            return false;
        } else {
            long fee = Long.parseLong(feeStr);
            // 用户费用小于每条短信单价
            if (fee < price) {
                sendMq.sendMq(RabbitMqConsants.TOPIC_SMS_SEND_LOG, submit);
                // 发送状态报告
                if (submit.getSource() != null && "1".equals(submit.getSource())) {
                    Standard_Report report = ReportMsg.sendReport(submit);
                    report.setErrorCode(StrategyConstants.STRATEGY_ERROR_FEE);
                    report.setState(2);
                    sendMq.sendMq(RabbitMqConsants.TOPIC_UPDATE_SMS_REPORT, report);
                }
                log.info("用户id为{}余额不足===>{}，账户已冻结", submit.getClientID(), feeStr);
                return false;
            }
            long decr = cacheFeign.decr(CacheConstants.CACHE_PREFIX_CUSTOMER_FEE + submit.getClientID(), price);
            if (decr >= 0){
                log.info("用户id为{}的账户已经扣费{}厘，余额为{}", submit.getClientID(), price, fee - price);
            }else {
                return false;
            }
        }
        return true;
    }
}
